package com.vito.oa.act.action;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.mq.ytx.MsgYtxGroupTools;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.model.OaActCommentModel;
import com.vito.oa.act.service.OaActCommentService;

/**
 * 审批评论
 * @author wangjie
 *
 */
@Controller
@RequestMapping("oa/act/comment")
public class OaActCommentAction extends BaseAction{
	
	private Logger log = LoggerFactory.getLogger(BaseAction.class);

	@Reference
	private OaActCommentService oaActCommentService;
	
	@ResponseBody
	@RequestMapping("saveComment")
	public Result saveComment(OaActCommentModel oaActCommentModel) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (StringUtils.isBlank(oaActCommentModel.getBusinessId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("业务id不能为空");
				return rs;
			}
			if (StringUtils.isBlank(oaActCommentModel.getCommentContent())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("内容不能为空");
				return rs;
			}
			oaActCommentModel.setCreateUser(user.getUserId());
			oaActCommentModel.setUserName(user.getUserName());
			oaActCommentModel.setSender(MsgYtxGroupTools.url.getProp("sender.approval"));
			boolean b = oaActCommentService.saveComment(oaActCommentModel);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("评论成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("评论失败");
			}
		} catch (Exception e) {
			log.error(ResMsg.EXECERR,e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	@ResponseBody
	@RequestMapping("getCommentApprovePage")
	public Result getCommentApprovePage(OaActExApproveInfoModel actExApproveInfoModel) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			actExApproveInfoModel.setUserid(user.getUserId());
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActCommentService.getCommentApprovePage(actExApproveInfoModel));
		} catch (Exception e) {
			log.error(ResMsg.EXECERR,e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	@ResponseBody
	@RequestMapping("getCommentList")
	public Result getCommentList(OaActCommentModel oaActCommentModel){
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			oaActCommentModel.setCreateUser(user.getUserId());
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActCommentService.getCommentList(oaActCommentModel));
		} catch (Exception e) {
			log.error(ResMsg.EXECERR,e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
		
	}
}
